import { get, post } from '@/api'
import { TResponse } from '@/typings'
import { encrypt } from '@/utils/rsaEncrypt'
import { TLoginCheck, TResponseCode, TResponseLogin, TResponseUserInfo } from './type'

const prefixUrl = '/auth/'

/** 用户验证码 */
export const code = (): Promise<TResponseCode> => {
  return get(`${prefixUrl}code`)
}

/** 根据token获取用户信息 */
// 发送请求时 后台自动解析token -> userId 根据userId查询用户信息
export const getUserInfo = (): Promise<TResponseUserInfo> => {
  return get(`${prefixUrl}getUserInfo`)
}

/** 用户登录 */
export const loginCheck = (loginCheck: TLoginCheck): Promise<TResponseLogin> => {
  const sendData: TLoginCheck = JSON.parse(JSON.stringify(loginCheck))
  // 密码加密
  sendData.password = encrypt(sendData.password)
  return post(`${prefixUrl}login`, sendData)
}

/** 用户退出 */
export const userLogout = (): Promise<TResponse<boolean>> => {
  return post(`${prefixUrl}logout`)
}
